// pages/receive/editBill/editBill.js
import api from '../../../utils/api';
import util from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: 'HTBH2020072721',
    canEdit: false,
    invoiceData: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.claimInvoiceId = options.claimInvoiceId;
    this.setData({
      canEdit: options.canEdit == 1 ? true : false
    })
    this.getData()
  },
  getData() {
    let that = this;
    api.getClaimInvoiceById({
      data: {
        claimInvoiceId: that.data.claimInvoiceId
      },
      success: function (res) {
        if (res.data.code) {
          res.data.data[0].money = util.formatFigure(res.data.data[0].money);
          res.data.data[0].claimedMoney = util.formatFigure(res.data.data[0].claimedMoney);
          res.data.data[0].remainingMoney = util.formatFigure(res.data.data[0].remainingMoney);
          res.data.data[0].claimMoney = that.data.canEdit ? res.data.data[0].claimMoney : util.formatFigure(res.data.data[0].claimMoney);
          that.getBasicData(res.data.data[0].claimId)
          that.setData({
            invoiceData: res.data.data[0]
          })
        }
      }
    })
  },
  // 基本信息
  getBasicData(claimId) {
    let that = this;
    api.getClaimById({
      data: {
        claimId: claimId
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            basicData: res.data.data[0]
          })
        }
      }
    })
  },
  // 备注
  onRemarksChange(e) {
    console.log(e)
    this.data.invoiceData.remarks = e.detail
  },

  onChange(e) {
    this.data.invoiceData.claimMoney = parseFloat(e.detail.replace("¥", "").replace(/\s*/g, ''));
  },
  // 保存
  onSave() {
    let that = this;
    console.log(that.data.invoiceData.claimMoney)
    api.editClaimInvoice({
      data: {
        id: that.data.invoiceData.id,
        money: that.data.invoiceData.claimMoney,
        remarks:that.data.invoiceData.remarks
      },
      method: 'POST',
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: '编辑成功',
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            })
          }, 100);
        } else {
          wx.showModal({
            content: res.data.message,
            showCancel: false,
            confirmColor: '#0041C4'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})